package com.ztao.controller;
import com.ztao.pojo.Goods;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FreemarkController {
//    @GetMapping("/t1")
//    public String test1(Model model){
//        Student student=new Student();
//        student.setName("哈哈哈");
//        student.setAge(18);
//        student.setSex("男");
//        List list=new ArrayList<>();
//        list.add(student);
//
//        Student student2=new Student();
//        student2.setName("哈哈哈");
//        student2.setAge(18);
//        student2.setSex("男");
//        list.add(student2);
//        model.addAttribute("student",list);
//        return "01-test";
//    }

    @Resource
    private Configuration configuration;
    @GetMapping("/goodsList")
    @ResponseBody
    public String list() throws IOException, TemplateException {
        Template template = configuration.getTemplate("02.ftl");
        Goods goods=new Goods();
        goods.setName("手机");
        goods.setPrice(1000);


        Goods goods2=new Goods();
        goods2.setName("沙发");
        goods2.setPrice(13000);

        List list=new ArrayList<>();
        list.add(goods);
        list.add(goods2);

        Map<String,Object> map=new HashMap<>();
        map.put("goods",list);
        map.put("goods2",goods2);

        template.process(map,new FileWriter("D:\\02.html"));
        return "成功";

    }


}
